{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用企业公示网做例子\n",
    "\n",
    "获取gt和challenge两个参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 企业公示网做例子\n",
    "index_url = 'http://www.gsxt.gov.cn/index.html'\n",
    "index_header= {\n",
    "            'accept': \"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3\",\n",
    "            'accept-encoding': \"gzip, deflate\",\n",
    "            'accept-language': \"zh-CN,zh;q=0.9\",\n",
    "            'connection': \"keep-alive\",\n",
    "            'host': \"www.gsxt.gov.cn\",\n",
    "            'upgrade-insecure-requests': \"1\",\n",
    "            'Referer': 'http://www.gsxt.gov.cn/index.html',\n",
    "            \"User-Agent\": \"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36\",\n",
    "        }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 代理手动配置，避免本地的IP被封了\n",
    "\n",
    "# 代理服务器\n",
    "proxyHost = \"forward.apeyun.com\"\n",
    "proxyPort = \"9082\"\n",
    "\n",
    "# 代理隧道验证信息\n",
    "proxyUser = \"2020070100845351047\"\n",
    "proxyPass = \"G8oyqzh6zswpSMtk\"\n",
    "\n",
    "proxyMeta = \"http://%(user)s:%(pass)s@%(host)s:%(port)s\" % {\n",
    "  \"host\" : proxyHost,\n",
    "  \"port\" : proxyPort,\n",
    "  \"user\" : proxyUser,\n",
    "  \"pass\" : proxyPass,\n",
    "}\n",
    "\n",
    "proxy = {\n",
    "    \"http\"  : \"\",\n",
    "    \"https\" : \"\",\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = requests.get(index_url, headers=index_header, proxies=proxy)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "获取到第一段JS代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'<script>var x=\"String@challenge@@charAt@@@innerHTML@Array@@Expires@07@document@3D@https@@false@RHu@@@return@for@@toString@toLowerCase@@reverse@captcha@@11@@@@Thu@href@replace@@RegExp@@@@@8@1500@onreadystatechange@search@@join@22@window@r@@length@pathname@createElement@@try@@0@@GMT@addEventListener@attachEvent@@47V@while@if@a@JgSe0upZ@@@@firstChild@@else@OXM@chars@0xEDB88320@998@Path@APPlY@1593670282@function@__jsl_clearance@0xFF@@split@DOMContentLoaded@var@1@36@@2@@@@f@Jul@@@charCodeAt@e@setTimeout@20@div@parseInt@3@location@@g@@rOm9XFMtA3QKV7nYsPGT4lifyWwkq5vcjH2IdxUoCbhERLaz81DNB6@d@@@cookie@match@02@DX@substr@@eval@new@@@@catch@4@fromCharCode\".replace(/@*$/,\"\").split(\"@\"),y=\"2a 36=24(){2o(\\'2t.y=2t.1e+2t.16.z(/[\\\\\\\\?|&]r-2/,\\\\\\\\\\'\\\\\\\\\\')\\',14);c.2B=\\'25=23.20|1j|\\'+(24(){2a w=[24(36){k 36},24(w){k w},(24(){2a 36=c.1f(\\'2q\\');36.7=\\'<1s y=\\\\\\\\\\'/\\\\\\\\\\'>1u</1s>\\';36=36.1x.y;2a w=36.2C(/e?:\\\\\\\\/\\\\\\\\//)[1j];36=36.32(w.1d).o();k 24(w){l(2a 1u=1j;1u<w.1d;1u++){w[1u]=36.4(w[1u])};k w.18(\\'\\')}})(),24(36){l(2a w=1j;w<36.1d;w++){36[w]=2r(36[w]).n(2c)};k 36.18(\\'\\')}],36=[\\'31\\',[(-~[2e]+[])+(([-~-~{}]+(+[])>>-~-~{})+[]+[[]][1j])],[(-~~~\\'\\'+[]+[])+(-~[2e]+[])],\\'1b\\',[(-~~~\\'\\'+[]+[])+(-~[2e]+[])],\\'1p\\',[-~![]-~[((+!(+!{}))<<(+!(+!{})))-~{}-~(-~[-~[]-~[]])]],[(3a+[]+[]),(-~[2e]+3a+[[]][1j])],[(-~[2e]+[])+(~~[]+[]+[]),(-~[2e]+[])+(3a+[]+[])],\\'1A\\',[(-~~~\\'\\'+[]+[])+(-~~~\\'\\'+[]+[])],\\'h\\',[(-~~~\\'\\'+[]+[])+(3a+[]+[])],\\'22%d\\'];l(2a 1u=1j;1u<36.1d;1u++){36[1u]=w[[2b,2s,2e,2b,2s,2b,1j,2e,2s,2b,2e,2b,2e,2b][1u]](36[1u])};k 36.18(\\'\\')})()+\\';a=x, 30-2j-2p b:t:19 1l;21=/;\\'};1r((24(){1h{k !!1a.1m;}39(2n){k g;}})()){c.1m(\\'29\\',36,g)}1z{c.1n(\\'15\\',36)}\",f=function(x,y){var a=0,b=0,c=0;x=x.split(\"\");y=y||99;while((a=x.shift())&&(b=a.charCodeAt(0)-77.5))c=(Math.abs(b)<13?(b+48.5):parseInt(a,36))+y*c;return c},z=f(y.match(/\\\\w/g).sort(function(x,y){return f(x)-f(y)}).pop());while(z++)try{eval(y.replace(/\\\\b\\\\w+\\\\b/g, function(y){return x[f(y,z)-1]||(\"_\"+y)}));break}catch(_){}</script>\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\n'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.text    # 获取第一段js"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "获取jsluid参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'697b3a8c209b3fcfafa830c6bc637392'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "jsluid = response.headers['Set-Cookie'].split('=')[1].split(';')[0]    \n",
    "jsluid"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "解密出第二段js代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"var _36=function(){setTimeout('location.href=location.pathname+location.search.replace(/[\\\\\\\\?|&]captcha-challenge/,\\\\\\\\'\\\\\\\\')',1500);document.cookie='__jsl_clearance=1593670282.998|0|'+(function(){var _w=[function(_36){return _36},function(_w){return _w},(function(){var _36=document.createElement('div');_36.innerHTML='<a href=\\\\\\\\'/\\\\\\\\'>_1u</a>';_36=_36.firstChild.href;var _w=_36.match(/https?:\\\\\\\\/\\\\\\\\//)[0];_36=_36.substr(_w.length).toLowerCase();return function(_w){for(var _1u=0;_1u<_w.length;_1u++){_w[_1u]=_36.charAt(_w[_1u])};return _w.join('')}})(),function(_36){for(var _w=0;_w<_36.length;_w++){_36[_w]=parseInt(_36[_w]).toString(36)};return _36.join('')}],_36=['DX',[(-~[2]+[])+(([-~-~{}]+(+[])>>-~-~{})+[]+[[]][0])],[(-~~~''+[]+[])+(-~[2]+[])],'r',[(-~~~''+[]+[])+(-~[2]+[])],'47V',[-~![]-~[((+!(+!{}))<<(+!(+!{})))-~{}-~(-~[-~[]-~[]])]],[(4+[]+[]),(-~[2]+4+[[]][0])],[(-~[2]+[])+(~~[]+[]+[]),(-~[2]+[])+(4+[]+[])],'OXM',[(-~~~''+[]+[])+(-~~~''+[]+[])],'RHu',[(-~~~''+[]+[])+(4+[]+[])],'APPlY%3D'];for(var _1u=0;_1u<_36.length;_1u++){_36[_1u]=_w[[1,3,2,1,3,1,0,2,3,1,2,1,2,1][_1u]](_36[_1u])};return _36.join('')})()+';Expires=Thu, 02-Jul-20 07:11:22 GMT;Path=/;'};if((function(){try{return !!window.addEventListener;}catch(e){return false;}})()){document.addEventListener('DOMContentLoaded',_36,false)}else{document.attachEvent('onreadystatechange',_36)}\""
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import re\n",
    "import execjs\n",
    "\n",
    "\n",
    "des_js = 'function get_z(x, y) {\\n    f = function (x, y) {\\n        var a = 0,\\n            b = 0,\\n            c = 0;\\n        x = x.split(\"\");\\n        y = y || 99;\\n        while ((a = x.shift()) && (b = a.charCodeAt(0) - 77.5)) c = (Math.abs(b) < 13 ? (b + 48.5) : parseInt(a, 36)) + y * c;\\n        return c\\n    };\\n    x = x.replace(/@*$/, \"\").split(\"@\");\\n    z = f(y.match(/\\\\w/g).sort(function (x, y) {\\n        return f(x) - f(y)\\n    }).pop());\\n    return [x, y, z]\\n\\n}\\n\\nfunction get_js(x, y, z) {\\n    f = function (x, y) {\\n        var a = 0,\\n            b = 0,\\n            c = 0;\\n        x = x.split(\"\");\\n        y = y || 99;\\n        while ((a = x.shift()) && (b = a.charCodeAt(0) - 77.5)) c = (Math.abs(b) < 13 ? (b + 48.5) : parseInt(a, 36)) + y * c;\\n        return c\\n    };\\n    x = eval(x);\\n    z = z + 1;\\n    p = y.replace(/\\\\b\\\\w+\\\\b/g,\\n        function (y) {\\n            return x[f(y, z) - 1] || (\"_\" + y)\\n        });\\n    return p\\n\\n}\\n\\n\\nfunction once_js(x, y) {\\n    f = function (x, y) {\\n        var a = 0,\\n            b = 0,\\n            c = 0;\\n        x = x.split(\"\");\\n        y = y || 99;\\n        while ((a = x.shift()) && (b = a.charCodeAt(0) - 77.5)) c = (Math.abs(b) < 13 ? (b + 48.5) : parseInt(a, 36)) + y * c;\\n        return c\\n    },\\n        x = x.replace(/@*$/, \"\").split(\"@\");\\n    z = f(y.match(/\\\\w/g).sort(function (x, y) {\\n        return f(x) - f(y)\\n    }).pop());\\n    while (z++) try {\\n        g = y.replace(/\\\\b\\\\w+\\\\b/g,\\n            function (y) {\\n                return x[f(y, z) - 1] || (\"_\" + y)\\n            });\\n        return g\\n    } catch (_) {\\n    }\\n}'\n",
    "des_js = execjs.compile(des_js)\n",
    "first_js = response.text\n",
    "x = re.findall('var x=\"(.*?)\"', first_js)[0]\n",
    "y = re.findall(',y=\"(.*?)\"', first_js)[0]\n",
    "second_js = des_js.call('once_js', x, y)\n",
    "second_js"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "处理第二段js，去除document"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'cookie=\\'__jsl_clearance=1593670282.998|0|\\'+(function(){var _w=[function(_36){return _36},function(_w){return _w},(function(){var _36=\"www.gsxt.gov.cn/index.html\";var _w=\"http://\";return function(_w){for(var _1u=0;_1u<_w.length;_1u++){_w[_1u]=_36.charAt(_w[_1u])};return _w.join(\\'\\')}})(),function(_36){for(var _w=0;_w<_36.length;_w++){_36[_w]=parseInt(_36[_w]).toString(36)};return _36.join(\\'\\')}],_36=[\\'DX\\',[(-~[2]+[])+(([-~-~{}]+(+[])>>-~-~{})+[]+[[]][0])],[(-~~~\\'\\'+[]+[])+(-~[2]+[])],\\'r\\',[(-~~~\\'\\'+[]+[])+(-~[2]+[])],\\'47V\\',[-~![]-~[((+!(+!{}))<<(+!(+!{})))-~{}-~(-~[-~[]-~[]])]],[(4+[]+[]),(-~[2]+4+[[]][0])],[(-~[2]+[])+(~~[]+[]+[]),(-~[2]+[])+(4+[]+[])],\\'OXM\\',[(-~~~\\'\\'+[]+[])+(-~~~\\'\\'+[]+[])],\\'RHu\\',[(-~~~\\'\\'+[]+[])+(4+[]+[])],\\'APPlY%3D\\'];for(var _1u=0;_1u<_36.length;_1u++){_36[_1u]=_w[[1,3,2,1,3,1,0,2,3,1,2,1,2,1][_1u]](_36[_1u])};return _36.join(\\'\\')})()+\\';Expires=Thu, 02-Jul-20 07:11:22 \\''"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_js = second_js.replace('\\\\\\\\', '\\\\')\n",
    "\n",
    "new_js = 'cookie' + new_js.split('document.cookie')[1]\n",
    "new_js = new_js.split('GMT;Path=/;')[0] + \"'\"\n",
    "\n",
    "if re.findall(\"(var .{0,5}=)document\\.createElement\\('div'\\);\", new_js):\n",
    "        _3d = re.findall(\"(var .{0,5}=)document\\.createElement\\('div'\\);\", new_js)\n",
    "        _2b = re.findall(\"(var .{0,5}=).{0,5}\\.match\\(/https\\?:\\\\\\/\\\\\\//\\)\\[0\\];\", new_js)\n",
    "        new_js = re.sub(\n",
    "            \"var .{0,5}=document\\.createElement\\('div'\\);\", \n",
    "            _3d[0] + f'\"{index_url.replace(\"http://\", \"\")}\";',\n",
    "            new_js\n",
    "        )\n",
    "        new_js = re.sub(\"_.{0,5}\\.innerHTML='<a href=.{0,25}</a>';\", \"\", new_js)\n",
    "        new_js = re.sub(\"_.{0,5}=.{0,5}\\.firstChild\\.href;\", \"\", new_js)\n",
    "        new_js = re.sub(\"var .{0,5}=.{0,5}\\.match\\(/https\\?:\\\\\\/\\\\\\//\\)\\[0\\];\", _2b[0] + '\"http://\";', new_js)\n",
    "        new_js = re.sub(\"_.{0,5}=.{0,5}\\.substr\\(.{0,5}\\.length\\)\\.toLowerCase\\(\\);\", \"\", new_js)\n",
    "new_js"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "处理js，去除Window"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "find = re.findall(\"!*window\\[.*?\\]\", new_js)\n",
    "if find:\n",
    "    for f in find:\n",
    "        if '!' in f:\n",
    "            if len(re.findall('!', f)) % 2 == 0:\n",
    "                new_js = new_js.replace(f, 'false')\n",
    "            else:\n",
    "                new_js = new_js.replace(f, 'true')\n",
    "        else:\n",
    "            new_js = new_js.replace(f, 'undefined')\n",
    "new_js = new_js.replace('window.headless', 'undefined')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'cookie=\\'__jsl_clearance=1593670282.998|0|\\'+(function(){var _w=[function(_36){return _36},function(_w){return _w},(function(){var _36=\"www.gsxt.gov.cn/index.html\";var _w=\"http://\";return function(_w){for(var _1u=0;_1u<_w.length;_1u++){_w[_1u]=_36.charAt(_w[_1u])};return _w.join(\\'\\')}})(),function(_36){for(var _w=0;_w<_36.length;_w++){_36[_w]=parseInt(_36[_w]).toString(36)};return _36.join(\\'\\')}],_36=[\\'DX\\',[(-~[2]+[])+(([-~-~{}]+(+[])>>-~-~{})+[]+[[]][0])],[(-~~~\\'\\'+[]+[])+(-~[2]+[])],\\'r\\',[(-~~~\\'\\'+[]+[])+(-~[2]+[])],\\'47V\\',[-~![]-~[((+!(+!{}))<<(+!(+!{})))-~{}-~(-~[-~[]-~[]])]],[(4+[]+[]),(-~[2]+4+[[]][0])],[(-~[2]+[])+(~~[]+[]+[]),(-~[2]+[])+(4+[]+[])],\\'OXM\\',[(-~~~\\'\\'+[]+[])+(-~~~\\'\\'+[]+[])],\\'RHu\\',[(-~~~\\'\\'+[]+[])+(4+[]+[])],\\'APPlY%3D\\'];for(var _1u=0;_1u<_36.length;_1u++){_36[_1u]=_w[[1,3,2,1,3,1,0,2,3,1,2,1,2,1][_1u]](_36[_1u])};return _36.join(\\'\\')})()+\\';Expires=Thu, 02-Jul-20 07:11:22 \\''"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_js"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "获取jsl_clearance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1593670282.998|0|DXzcrd47V9gtuyOXMvRHunAPPlY%3D'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "js = \"\"\"\n",
    "        function getJS() {\n",
    "        %s\n",
    "        return cookie\n",
    "        }\n",
    "    \"\"\" % new_js\n",
    "exec_js = execjs.compile(js)\n",
    "jsl = exec_js.call('getJS')\n",
    "jsl = jsl.split(';')[0]\n",
    "jsl_clearance = jsl.split('=')[1]\n",
    "jsl_clearance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'e1d92a59a5b23981cbfa00048eea'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取第一个jessionid\n",
    "headers = index_header.copy()\n",
    "headers['Cookie'] = f'__jsluid_h={jsluid}; __jsl_clearance={jsl_clearance};'\n",
    "response2 = requests.get(index_url, headers=headers, proxies=proxy)\n",
    "JSESSIONID1 = re.findall('JSESSIONID=(.*?);', response2.headers['Set-Cookie'])[0]\n",
    "JSESSIONID1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'e1d92a59a5b23981cbfa00048eea'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取第二个jessionid\n",
    "import time\n",
    "\n",
    "url = 'http://www.gsxt.gov.cn/SearchItemCaptcha?t=' + str(int(time.time()*1000))\n",
    "headers = index_header.copy()\n",
    "headers['Cookie'] = f'__jsluid_h={jsluid}; __jsl_clearance={jsl_clearance};JSESSIONID={JSESSIONID1}'\n",
    "response3 = requests.get(url, headers=headers, proxies=proxy)\n",
    "JSESSIONID2 = re.findall('JSESSIONID=(.*?);', response3.headers['Set-Cookie'])[0]\n",
    "JSESSIONID2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "测试cookie，如果状态码是200，获取gt和challenge"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "headers = index_header.copy()\n",
    "headers['Cookie'] = f'__jsluid_h={jsluid}; __jsl_clearance={jsl_clearance};JSESSIONID={JSESSIONID2}'\n",
    "url = 'http://www.gsxt.gov.cn/SearchItemCaptcha?t=' + str(int(time.time()*1000))\n",
    "response4 = requests.get(url, headers=headers, proxies=proxy)\n",
    "response4.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "gt_challenge_json = json.loads(response4.text)\n",
    "gt = gt_challenge_json['gt']\n",
    "challenge = gt_challenge_json['challenge']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'62756445cd524543f5a16418cd920ffd'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'bc7be94649e930d1f8da98772e82060f'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "challenge"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 下面开始搜索企业信息"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 如果您看到这里，请给我一个star，谢谢，如有遗漏，请留言指正，感激！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里先走一遍采集流程，本地打码服务的实现稍后整理，要先整理完模型训练部分才能打码\n",
    "服务实现流程：\n",
    "1. 拿到gt和challenge获取验证码，验证码有两种\n",
    "    - 滑动3.0\n",
    "    - 文字点选，这部分的训练模型已经开源了，移步首页\n",
    "2. 破解验证码，获取validate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 本地打码服务器，所有验证码类型在这里统一处理，返回validate即可\n",
    "get_valid_server = 'http://49.235.209.176:5050'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "ename": "JSONDecodeError",
     "evalue": "Expecting value: line 1 column 1 (char 0)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mJSONDecodeError\u001b[0m                           Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-19-b28b7f0729d9>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     11\u001b[0m \u001b[0mresp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrequests\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpost\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mjson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdumps\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjson\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/opt/anaconda3/envs/blog/lib/python3.7/site-packages/requests/models.py\u001b[0m in \u001b[0;36mjson\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m    896\u001b[0m                     \u001b[0;31m# used.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    897\u001b[0m                     \u001b[0;32mpass\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 898\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0mcomplexjson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mloads\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtext\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    899\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    900\u001b[0m     \u001b[0;34m@\u001b[0m\u001b[0mproperty\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/opt/anaconda3/envs/blog/lib/python3.7/json/__init__.py\u001b[0m in \u001b[0;36mloads\u001b[0;34m(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)\u001b[0m\n\u001b[1;32m    346\u001b[0m             \u001b[0mparse_int\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mparse_float\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m \u001b[0;32mand\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    347\u001b[0m             parse_constant is None and object_pairs_hook is None and not kw):\n\u001b[0;32m--> 348\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0m_default_decoder\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdecode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    349\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mcls\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    350\u001b[0m         \u001b[0mcls\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mJSONDecoder\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/opt/anaconda3/envs/blog/lib/python3.7/json/decoder.py\u001b[0m in \u001b[0;36mdecode\u001b[0;34m(self, s, _w)\u001b[0m\n\u001b[1;32m    335\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    336\u001b[0m         \"\"\"\n\u001b[0;32m--> 337\u001b[0;31m         \u001b[0mobj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mraw_decode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0midx\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0m_w\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    338\u001b[0m         \u001b[0mend\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_w\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    339\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mend\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/opt/anaconda3/envs/blog/lib/python3.7/json/decoder.py\u001b[0m in \u001b[0;36mraw_decode\u001b[0;34m(self, s, idx)\u001b[0m\n\u001b[1;32m    353\u001b[0m             \u001b[0mobj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mscan_once\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0midx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    354\u001b[0m         \u001b[0;32mexcept\u001b[0m \u001b[0mStopIteration\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0merr\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 355\u001b[0;31m             \u001b[0;32mraise\u001b[0m \u001b[0mJSONDecodeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Expecting value\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0merr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    356\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0mobj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mJSONDecodeError\u001b[0m: Expecting value: line 1 column 1 (char 0)"
     ]
    }
   ],
   "source": [
    "# url = f'{get_valid_server}/geetest?gt={gt}&challenge={challenge}'\n",
    "# resp = requests.get(url)\n",
    "# resp\n",
    "\n",
    "data = {\n",
    "    'gt': gt,\n",
    "    'challenge': challenge\n",
    "}\n",
    "url = 'http://3215952j9f.qicp.vip:10682/captcha'\n",
    "\n",
    "resp = requests.post(url, data=json.dumps(data))\n",
    "print(resp.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "val_resu = json.loads(resp.text)\n",
    "\n",
    "# def get_validate_wander(challenge,gt, vtype=1):\n",
    "#     data = {\n",
    "#         'gt':gt,\n",
    "#         'challenge':challenge\n",
    "#     }\n",
    "#     url='http://49.235.179.162:5000/geetest'\n",
    "#     response = requests.post(url=url,data=json.dumps(data))\n",
    "#     return response.json()\n",
    "\n",
    "# val_resu = get_validate_wander(challenge=challenge, gt=gt)\n",
    "val_resu"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "成功获取validate，开始进行搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from lxml.etree import _Element\n",
    "from lxml import etree\n",
    "\n",
    "def parse_list(content):\n",
    "    def try_catch(data, patten):\n",
    "        if isinstance(data, _Element):\n",
    "            try:\n",
    "                target = re.sub('\\s', '', data.xpath(patten)[0].strip())\n",
    "            except:\n",
    "                target = None\n",
    "            return target\n",
    "        return data\n",
    "    a_list = []\n",
    "    content_list = content\n",
    "    html = etree.HTML(content_list)\n",
    "    list_len = int(html.xpath('//span[@class=\"search_result_span1\"]/text()')[0])\n",
    "    if list_len == 0:\n",
    "        return 'None'\n",
    "    else:\n",
    "        targit_len = 1\n",
    "    for i in range(1, targit_len + 1):\n",
    "        try:\n",
    "            url = html.xpath('//div[@class=\"main-layout fw f14\"]/div[2]/a[' + str(i) + ']/@href')[0]\n",
    "            href = 'http://www.gsxt.gov.cn{}'.format(url)\n",
    "        except:\n",
    "            href = None\n",
    "        dic = {\n",
    "            'href': href\n",
    "        }\n",
    "        a_list.append(dic)\n",
    "    return a_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def list_page(val_resu, jsluid, jsl_clearance, JSESSIONID, search_word):\n",
    "    time.sleep(1)\n",
    "    validate = val_resu['data']['validate']\n",
    "    chall = val_resu['data']['challenge']\n",
    "    data = {\n",
    "        'tab': 'ent_tab',\n",
    "        'province': '',\n",
    "        'geetest_challenge': chall,\n",
    "        'geetest_validate': validate,\n",
    "        'geetest_seccode': '{}|jordan'.format(validate),\n",
    "        'token': '57571798',\n",
    "        'searchword': f'{search_word}',\n",
    "    }\n",
    "    time.sleep(5)\n",
    "    headers =index_header.copy()\n",
    "    headers['Cookie'] = f'__jsluid_h={jsluid}; __jsl_clearance={jsl_clearance};JSESSIONID={JSESSIONID}'\n",
    "    post_url = 'http://www.gsxt.gov.cn/corp-query-search-1.html'\n",
    "    content = requests.post(url=post_url, data=data, headers=headers, verify=False, timeout=(60, 60), proxies=proxy)\n",
    "    if content.status_code == 200:\n",
    "        if '您的IP地址' in content.text:\n",
    "            print('请更换代理')\n",
    "        else:\n",
    "            return content.text\n",
    "    else:\n",
    "        print('cookies失效')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "配置要搜索的关键词"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "search_word = \"百度\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if val_resu['data']['result'] == 'fail':\n",
    "    print('get valid error')\n",
    "else:\n",
    "    content_text = list_page(val_resu, jsluid, jsl_clearance, JSESSIONID2, search_word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "href = parse_list(content_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "href"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "headers =index_header.copy()\n",
    "headers['Cookie'] = f'__jsluid_h={jsluid}; __jsl_clearance={jsl_clearance};JSESSIONID={JSESSIONID2}'\n",
    "detail = requests.get(url=href[0]['href'], headers = headers, proxies = proxy)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出结果正确"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "etree.HTML(detail.text).xpath(\"//h1\")[0].text.strip()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 如果您看到这里，请给我一个star，谢谢，如有遗漏，请留言指正，感激！"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "blog",
   "language": "python",
   "name": "blog"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
